/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_DECODER) || \
    defined(UMC_ENABLE_AVS_VIDEO_ENCODER)

#include "umc_avs_fusion_core.h"

#include "umc_avs_pic.h"

namespace UMC
{

Status AVSFusionCore::InitializeFrame(AVSFrame *pFrame)
{
    Status umcRes;

    pFrame->m_seqHeader = m_seqHeader;
    pFrame->m_picHeader = m_picHeader;

    // initialize the frame
    umcRes = pFrame->Init();

    return umcRes;

} // Status AVSFusionCore::InitializeFrame(AVSFrame *pFrame)

Status AVSFusionCore::PurgeFrame(AVSFrame *pFrame)
{
    // remove all items from the frame
    pFrame->RemoveSlices(&m_FreeSlices);

    pFrame->RemoveMemoryPieces(&m_FreeMemory);

    // reset all flags
    pFrame->Reset();

    return UMC_OK;

} // Status AVSFusionCore::PurgeFrame(AVSFrame *pFrame)

static
Ipp32s InitializeReferenceList(AVSFrame **pRefList, AVSListElement<AVSFrame> *pFrameList)
{
    Ipp32s numRefs = 0;

    // reset the reference list
    pRefList[0] = NULL;
    pRefList[1] = NULL;

    while (pFrameList)
    {
        if (pFrameList->IsReference())
        {
            pRefList[1] = pRefList[0];
            pRefList[0] = pFrameList;
            numRefs += 1;
        }

        // go to the next frame in the list
        pFrameList = pFrameList->GetNext();
    }

    return IPP_MIN(2, numRefs);

} // Ipp32s InitializeReferenceList(AVSFrame **pRefList, AVSListElement<AVSFrame> *pFrameList)

Status AVSFusionCore::CreateReferenceLists(AVSFrame *pFrame)
{
    AVSFrame *(refs[2]);

    if (AVS_I_PICTURE != pFrame->m_picHeader.PictureType)
    {
        Ipp32s numRefs;

        numRefs = InitializeReferenceList(refs, m_Frames.GetHead());
        if (0 == numRefs)
        {
            return UMC_ERR_INIT;
        }
        else if (1 == numRefs)
        {
            refs[1] = refs[0];
        }
    }
    else
    {
        refs[0] = refs[1] = 0;
    }

    pFrame->SetReferences(refs);

    return UMC_OK;

} // Status AVSFusionCore::CreateReferenceLists(AVSFrame *pFrame)

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_DECODER) ||
